﻿using System;
using System.Collections;
using System.IO;
using TotalCommander.Plugin.Wfx;
using NI.Vfs;
using HtmlAgilityPack;

namespace TotalCommander.Plugin.WywrotaExplorer.Wfx
{
    public class WywrotaExplorerWfxPlugin : TotalCommanderWfxPlugin
    {
        private IFileSystem fileSystem = new LocalFileSystem("D:\\");
        private HtmlWeb hw = new HtmlWeb();
        private HtmlDocument hd = new HtmlDocument();
        private HtmlNode[][] menus= new HtmlNode[4][];
        private string[] sciezki = new string[4];
        private int currLevel = 0;
        private HtmlDataManager hmd;

        public override string PluginName
        {
            get { return "Wywrota Explorer Plugin"; }
        }

        public override FindData FindFirst(string path, out IEnumerator enumerator)
        {
            //string[] tokeny = SplitPath(path);
            int level = SplitPath(path).Length;
            //currLevel = level;
            string href = String.Empty;
            if (path != "\\")
            {
                HtmlNode node = hmd.GetNode(path);
            }
            enumerator = hmd.Menus[level].GetEnumerator();
            if (enumerator.MoveNext()) return hmd.ToFindData((HtmlNode)enumerator.Current);
            enumerator = null;
            return FindData.NoMoreFiles;
        }

        private static string[] SplitPath(string path)
        {
            string[] tokeny = path.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
            return tokeny;
        }

        public override FindData FindNext(IEnumerator enumerator)
        {
            if (enumerator != null && enumerator.MoveNext())
            {
                    return hmd.ToFindData((HtmlNode)enumerator.Current);
            }
            return FindData.NoMoreFiles;
        }

        //public override bool DirectoryCreate(string path)
        //{
        //    try
        //    {
        //        GetNode(path).CreateFolder();
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        //public override bool DirectoryRemove(string remoteName)
        //{
        //    try
        //    {
        //        GetNode(remoteName).Delete();
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        public override ExecuteResult ExecuteOpen(TotalCommanderWindow window, ref string remoteName)
        {
            Request.MessageBox("path exec: " + remoteName);
            Request.MessageBox(hmd.GetNode(remoteName).InnerText);
            return ExecuteResult.OK;
        }

        public override void Initialize()
        {
            hmd = new HtmlDataManager("");
            hw = new HtmlWeb();
            HtmlDocument hdd = new HtmlDocument();
        }

        public override TotalCommander.Plugin.Wfx.FileOperationResult FileCopy(string source, string target, bool overwrite, bool move, TotalCommander.Plugin.Wfx.RemoteInfo ri)
        {
            Request.MessageBox("copy");
            throw new System.NotImplementedException();
        }

        public override TotalCommander.Plugin.Wfx.FileOperationResult FileGet(string remoteName, ref string localName, TotalCommander.Plugin.Wfx.CopyFlags copyFlags, TotalCommander.Plugin.Wfx.RemoteInfo ri)
        {
            localName += ".html";
            var file = fileSystem.ResolveFile(localName);
            file.CreateFile();
            string data = hmd.GetRemoteData(remoteName);
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
            file.CopyFrom(new MemoryStream(byteArray));
            file.Close();
            return FileOperationResult.OK;
        }
    }
}
